﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.IO.IsolatedStorage;
using System.IO;
using System.Data.SqlClient;
//using RESP_add;


namespace RESP_IIWaterAssessment
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            Boolean check_con = true;
            this.Cursor = Cursors.WaitCursor;
            try
            {
                DBClass.Connection();
                DBClass.FillTables();
            }
            catch (Exception ex)
            {
                MessageBox.Show(msgClass.msgError);
                this.Close();
                //check_con = false;
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
            if (check_con)
            {
                if (!InformationClass.Authorization())
                {
                    if (InformationClass.Lang)
                        MessageBox.Show("Авторизация не пройдена. Приложение будет закрыто");
                    else
                        MessageBox.Show("Авторизациядан ўтмади. Илова ёпилади.");
                    System.Environment.Exit(0);
                }
            }
            if (!InformationClass.isAdmin)
            {
                regionsToolStripMenuItem.Visible = uISesToolStripMenuItem.Visible = dictionarysToolStripMenuItem.Visible =
                    useOfSeedToolStripMenuItem1.Visible = usersToolStripMenuItem.Visible = jornalToolStripMenuItem.Visible =
                    trainingsToolStripMenuItem.Visible = monitoringTableToolStripMenuItem.Visible = trainingsToolStripMenuItem1.Visible =
                    indicatorsToolStripMenuItem.Visible = MonitoringARSPToolStripMenuItem.Visible = false;
            }

            jornalToolStripMenuItem.Visible = InformationClass.ShowJornal;

            if (InformationClass.IsTest)
            {
                dictionarysToolStripMenuItem.Visible = iinstitutionalToolStripMenuItem.Visible = engineeringPerformanceToolStripMenuItem.Visible =
                    economicIndexesToolStripMenuItem.Visible = monitoringTableToolStripMenuItem.Visible =
                    usersToolStripMenuItem.Visible = exportToolStripMenuItem.Visible = false;
            }




            
        }

        private void conToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // **************   Вписать имя базы
            //
            NewConectToServerForm newConnect = new NewConectToServerForm();
            if (!ContainsForm(newConnect.GetType()))
            {
                newConnect.MdiParent = this;
                newConnect.Show();
            }
        }

        private void dictionarysToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //TestForm tf = new TestForm();
            //if (!ContainsForm(tf))
            //{                
            //    tf.MdiParent = this;
            //    tf.Show();
            //}
        }

        private void iinstitutionalToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private bool ContainsForm(Type typeForm)
        {

            foreach (Form item in this.MdiChildren)
            {
                if (item.GetType() == typeForm)
                    return true;
            }

            return false;
        }

        private void languageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UtilClass.EditLanguage();
        }

        private void exportToolStripMenuItem_Click(object sender, EventArgs e)
        {

            ExportForm export = new ExportForm();
            if (!ContainsForm(export.GetType()))
            {
                export.MdiParent = this;
                export.Show();
            }

        }

        private void AVPToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AVPForm AVPform = new AVPForm();
            if (!ContainsForm(AVPform.GetType()))
            {
                AVPform.MdiParent = this;
                AVPform.Show();
            }
        }

        private void canalsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowCanalsForm canalForm = new ShowCanalsForm();
            if (!ContainsForm(canalForm.GetType()))
            {
                canalForm.MdiParent = this;
                canalForm.Show();
            }
        }

        private void farmersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FarmersForm farmerForm = new FarmersForm();
            if (!ContainsForm(farmerForm.GetType()))
            {
                farmerForm.MdiParent = this;
                farmerForm.Show();
            }
        }

        private void regionsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            RegionForm regionForm = new RegionForm();
            if (!ContainsForm(regionForm.GetType()))
            {
                regionForm.MdiParent = this;
                regionForm.Show();
            }
        }

        private void pFSHToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PFSHForm pfshForm = new PFSHForm();
            if (!ContainsForm(pfshForm.GetType()))
            {
                pfshForm.MdiParent = this;
                pfshForm.Show();
            }
        }

        private void otherToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CategoryesForm catForm = new CategoryesForm();
            if (!ContainsForm(catForm.GetType()))
            {
                catForm.MdiParent = this;
                catForm.Show();
            }
        }

        private void contractsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ContractsForm contractsForm = new ContractsForm();
            if (!ContainsForm(contractsForm.GetType()))
            {
                contractsForm.MdiParent = this;
                contractsForm.Show();
            }
        }

        private void cleaningsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            HasharForm hasharForm = new HasharForm();
            if (!ContainsForm(hasharForm.GetType()))
            {
                hasharForm.MdiParent = this;
                hasharForm.Show();
            }
        }

        private void contractsAndCleanungsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            HashardAndContractsReport HashAndContRep = new HashardAndContractsReport();
            if (!ContainsForm(HashAndContRep.GetType()))
            {
                HashAndContRep.MdiParent = this;
                HashAndContRep.Show();
            }
        }

        private void membersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MembersForm memForm = new MembersForm();
            if (!ContainsForm(memForm.GetType()))
            {
                memForm.MdiParent = this;
                memForm.Show();
            }
        }

        private void meetingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MeetingAVPForm mAVPForm = new MeetingAVPForm();
            if (!ContainsForm(mAVPForm.GetType()))
            {
                mAVPForm.MdiParent = this;
                mAVPForm.Show();
            }
        }

        private void commissionsAndMeetingTypesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MeetingsTypesForm mtForm = new MeetingsTypesForm();
            if (!ContainsForm(mtForm.GetType()))
            {
                mtForm.MdiParent = this;
                mtForm.Show();
            }
        }

        private void equipmentToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CanalStateForm canState = new CanalStateForm();
            if (!ContainsForm(canState.GetType()))
            {
                canState.MdiParent = this;
                canState.Show();
            }
        }

        private void executivePowersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ExecutivePowersReport execRep = new ExecutivePowersReport();
            if (!ContainsForm(execRep.GetType()))
            {
                execRep.MdiParent = this;
                execRep.Show();
            }
        }

        private void conflictsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ConflictReportForm crf = new ConflictReportForm();
            if (!ContainsForm(crf.GetType()))
            {
                crf.MdiParent = this;
                crf.Show();
            }
        }

        private void techniqueBalanceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TechniqueForm techForm = new TechniqueForm();
            if (!ContainsForm(techForm.GetType()))
            {
                techForm.MdiParent = this;
                techForm.Show();
            }
        }

        private void drainageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DrainagesForm drForm = new DrainagesForm();
            if (!ContainsForm(drForm.GetType()))
            {
                drForm.MdiParent = this;
                drForm.Show();
            }
        }

        private void productsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ProductionForm prodForm = new ProductionForm();
            if (!ContainsForm(prodForm.GetType()))
            {
                prodForm.MdiParent = this;
                prodForm.Show();
            }
        }

        private void generalMeetingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MeetingsReport mRep = new MeetingsReport();
            if (!ContainsForm(mRep.GetType()))
            {
                mRep.MdiParent = this;
                mRep.Show();
            }
        }

        private void agricultureToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CultureForm cultForm = new CultureForm();
            if (!ContainsForm(cultForm.GetType()))
            {
                cultForm.MdiParent = this;
                cultForm.Show();
            }
        }

        private void fertilizersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CategoryesForm catForm = new CategoryesForm("Fertilizer");
            if (!ContainsForm(catForm.GetType()))
            {
                catForm.MdiParent = this;
                catForm.Show();
            }
        }

        private void protectorsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PlantProtectionKindForm PlantProtectForm = new PlantProtectionKindForm();
            if (!ContainsForm(PlantProtectForm.GetType()))
            {
                PlantProtectForm.MdiParent = this;
                PlantProtectForm.Show();
            }
        }

        private void trainingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TrainingForm trFrom = new TrainingForm(false);
            if (!ContainsForm(trFrom.GetType()))
            {
                trFrom.MdiParent = this;
                trFrom.Show();
            }
        }

        private void profitAVPToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AVPIncomeForm avpIncomeForm = new AVPIncomeForm();
            if (!ContainsForm(avpIncomeForm.GetType()))
            {
                avpIncomeForm.MdiParent = this;
                avpIncomeForm.Show();
            }

        }

        private void costGMSToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GMSBalanceForm gmsBalForm = new GMSBalanceForm(false);
            if (!ContainsForm(gmsBalForm.GetType()))
            {
                gmsBalForm.MdiParent = this;
                gmsBalForm.Show();
            }
        }

        private void monitoringBalanceGMSToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GMSBalanceForm gmsBalForm = new GMSBalanceForm(true);
            if (!ContainsForm(gmsBalForm.GetType()))
            {
                gmsBalForm.MdiParent = this;
                gmsBalForm.Show();
            }
        }

        private void grossProfitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CultureProfitForm culProf = new CultureProfitForm();
            if (!ContainsForm(culProf.GetType()))
            {
                culProf.MdiParent = this;
                culProf.Show();
            }
        }

        private void activitiesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FarmIndicatorsForm farmIndForm = new FarmIndicatorsForm();
            if (!ContainsForm(farmIndForm.GetType()))
            {
                farmIndForm.MdiParent = this;
                farmIndForm.Show();
            }
        }

        private void activitiesReportToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FarmIndicatorsRepForm FIRepForm = new FarmIndicatorsRepForm();
            if (!ContainsForm(FIRepForm.GetType()))
            {
                FIRepForm.MdiParent = this;
                FIRepForm.Show();
            }
        }

        private void mechanicalWorkToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            MechanicalWorkForm MWForm = new MechanicalWorkForm();
            if (!ContainsForm(MWForm.GetType()))
            {
                MWForm.MdiParent = this;
                MWForm.Show();
            }
        }

        private void meetingsToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            ViewMeetingsForm viewMeetingForm = new ViewMeetingsForm(true);
            if (!ContainsForm(viewMeetingForm.GetType()))
            {
                viewMeetingForm.MdiParent = this;
                viewMeetingForm.Show();
            }
        }

        private void manualLabourToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            HandWorkForm hwForm = new HandWorkForm();
            if (!ContainsForm(hwForm.GetType()))
            {
                hwForm.MdiParent = this;
                hwForm.Show();
            }
        }

        private void useOfSeedToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            SchoolSeedForm schSeedForm = new SchoolSeedForm();
            if (!ContainsForm(schSeedForm.GetType()))
            {
                schSeedForm.MdiParent = this;
                schSeedForm.Show();
            }
        }

        private void waterManagementToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FieldWaterUseForm fieldWUForm = new FieldWaterUseForm();
            if (!ContainsForm(fieldWUForm.GetType()))
            {
                fieldWUForm.MdiParent = this;
                fieldWUForm.Show();
            }
        }

        private void fertilizerUsageToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            SchoolFertilizerForm schoolFertilizerForm = new SchoolFertilizerForm();
            if (!ContainsForm(schoolFertilizerForm.GetType()))
            {
                schoolFertilizerForm.MdiParent = this;
                schoolFertilizerForm.Show();
            }
        }

        private void waterFeedToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WaterSupplyForm waterSForm = new WaterSupplyForm();
            if (!ContainsForm(waterSForm.GetType()))
            {
                waterSForm.MdiParent = this;
                waterSForm.Show();
            }
        }

        private void indicatorsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IndikatorsForm indForm = new IndikatorsForm();
            if (!ContainsForm(indForm.GetType()))
            {
                indForm.MdiParent = this;
                indForm.Show();
            }
        }

        private void trainingsToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            TrainingForm trFrom = new TrainingForm(true);
            if (!ContainsForm(trFrom.GetType()))
            {
                trFrom.MdiParent = this;
                trFrom.Show();
            }
        }

        private void aVPHarvestToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AVPHarvestForm avpHarveatForm = new AVPHarvestForm();
            if (!ContainsForm(avpHarveatForm.GetType()))
            {
                avpHarveatForm.MdiParent = this;
                avpHarveatForm.Show();
            }
        }

        private void productionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SchoolProductionForm schoolProductionForm = new SchoolProductionForm();
            if (!ContainsForm(schoolProductionForm.GetType()))
            {
                schoolProductionForm.MdiParent = this;
                schoolProductionForm.Show();
            }
        }

        private void productionReportToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SchoolProductionFormReport schProdReport = new SchoolProductionFormReport();
            if (!ContainsForm(schProdReport.GetType()))
            {
                schProdReport.MdiParent = this;
                schProdReport.Show();
            }
        }

        private void fixedCostsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SchoolLossForm schoolLossForm = new SchoolLossForm();
            if (!ContainsForm(schoolLossForm.GetType()))
            {
                schoolLossForm.MdiParent = this;
                schoolLossForm.Show();
            }
        }

        private void lossesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LossForm lForm = new LossForm();
            if (!ContainsForm(lForm.GetType()))
            {
                lForm.MdiParent = this;
                lForm.Show();
            }
        }

        private void fixedCostsReportToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SchoolLossFormReport lossRep = new SchoolLossFormReport();
            if (!ContainsForm(lossRep.GetType()))
            {
                lossRep.MdiParent = this;
                lossRep.Show();
            }
        }

        private void plantProtectionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PlantProtectionForm plprForm = new PlantProtectionForm();
            if (!ContainsForm(plprForm.GetType()))
            {
                plprForm.MdiParent = this;
                plprForm.Show();
            }
        }

        private void schoolUsageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UsageReportsForm usageRepForm = new UsageReportsForm();
            if (!ContainsForm(usageRepForm.GetType()))
            {
                usageRepForm.MdiParent = this;
                usageRepForm.Show();
            }
        }

        private void waterFeedReportToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WaterSupplyFormReport wsRep = new WaterSupplyFormReport();
            if (!ContainsForm(wsRep.GetType()))
            {
                wsRep.MdiParent = this;
                wsRep.Show();
            }
        }

        private void financePlanToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FinancePlanForm finplanForm = new FinancePlanForm();
            if (!ContainsForm(finplanForm.GetType()))
            {
                finplanForm.MdiParent = this;
                finplanForm.Show();
            }
        }

        private void financePlanReportToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FinancePlanReportForm finPlanReport = new FinancePlanReportForm();
            if (!ContainsForm(finPlanReport.GetType()))
            {
                finPlanReport.MdiParent = this;
                finPlanReport.Show();
            }
        }

        private void channelStateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StateCoverForm stateCoverForm = new StateCoverForm();
            if (!ContainsForm(stateCoverForm.GetType()))
            {
                stateCoverForm.MdiParent = this;
                stateCoverForm.Show();
            }
        }

        private void monitoringProfitAVPToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AVPIncomeFormReport avpIncomeReport = new AVPIncomeFormReport();
            if (!ContainsForm(avpIncomeReport.GetType()))
            {
                avpIncomeReport.MdiParent = this;
                avpIncomeReport.Show();
            }
        }

        private void equipmentDrainageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DrainageStateForm drStateForm = new DrainageStateForm();
            if (!ContainsForm(drStateForm.GetType()))
            {
                drStateForm.MdiParent = this;
                drStateForm.Show();
            }
        }

        private void equipmentReportToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CanalStateFormReport canStRep = new CanalStateFormReport();
            if (!ContainsForm(canStRep.GetType()))
            {
                canStRep.MdiParent = this;
                canStRep.Show();
            }
        }

        private void MonitoringARSPToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IndikatorFormReport indRepForm = new IndikatorFormReport();
            if (!ContainsForm(indRepForm.GetType()))
            {
                indRepForm.MdiParent = this;
                indRepForm.Show();
            }
        }

        private void equipmentDrainageReportToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            DrainageStateFormReport drStReport = new DrainageStateFormReport();
            if (!ContainsForm(drStReport.GetType()))
            {
                drStReport.MdiParent = this;
                drStReport.Show();
            }
        }

        private void salinityAndSaltinessToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MineralizationForm minForm = new MineralizationForm();
            if (!ContainsForm(minForm.GetType()))
            {
                minForm.MdiParent = this;
                minForm.Show();
            }
        }

        private void techniqueBalanceReportToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TechBalanceFormReport techBalanceReport = new TechBalanceFormReport();
            if (!ContainsForm(techBalanceReport.GetType()))
            {
                techBalanceReport.MdiParent = this;
                techBalanceReport.Show();
            }
        }

        private void salinityAndSaltinessReportToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MineralizationFormReport minReport = new MineralizationFormReport();
            if (!ContainsForm(minReport.GetType()))
            {
                minReport.MdiParent = this;
                minReport.Show();
            }
        }

        private void agroeconomicIndicatorReportToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FarmAgroIndicatorFormReport fAgroIndRep = new FarmAgroIndicatorFormReport();
            if (!ContainsForm(fAgroIndRep.GetType()))
            {
                fAgroIndRep.MdiParent = this;
                fAgroIndRep.Show();
            }
        }

        private void agroeconomicIndicatorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FarmAgroIndicatorForm fAgroIndForm = new FarmAgroIndicatorForm();
            if (!ContainsForm(fAgroIndForm.GetType()))
            {
                fAgroIndForm.MdiParent = this;
                fAgroIndForm.Show();
            }
        }

        private void channelStateReportToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StateCoverFormReport stCovRep = new StateCoverFormReport();
            if (!ContainsForm(stCovRep.GetType()))
            {
                stCovRep.MdiParent = this;
                stCovRep.Show();
            }
        }

        private void aVPHarvestReportToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AVPHarvestFormReport avpHarRep = new AVPHarvestFormReport();
            if (!ContainsForm(avpHarRep.GetType()))
            {
                avpHarRep.MdiParent = this;
                avpHarRep.Show();
            }
        }

        private void uISesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UISForm uisForm = new UISForm();
            if (!ContainsForm(uisForm.GetType()))
            {
                uisForm.MdiParent = this;
                uisForm.Show();
            }
        }

        private void gMSTypesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CategoryesForm catForm = new CategoryesForm("GMSType");
            if (!ContainsForm(catForm.GetType()))
            {
                catForm.MdiParent = this;
                catForm.Show();
            }
        }

        private void actionsPFShToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CategoryesForm catForm = new CategoryesForm("OperationKind");
            if (!ContainsForm(catForm.GetType()))
            {
                catForm.MdiParent = this;
                catForm.Show();
            }
        }

        private void technicsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CategoryesForm catForm = new CategoryesForm("TechKind");
            if (!ContainsForm(catForm.GetType()))
            {
                catForm.MdiParent = this;
                catForm.Show();
            }
        }

        private void monitoringTableToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MonitoringForm monitoringReport = new MonitoringForm();
            if (!ContainsForm(monitoringReport.GetType()))
            {
                monitoringReport.MdiParent = this;
                monitoringReport.Show();
            }
        }

        private void importToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ImportForm importForm = new ImportForm();
            importForm.ShowDialog();
        }

        private void grossProfitReportToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CultureProfitFormReport cpfRep = new CultureProfitFormReport();
            if (!ContainsForm(cpfRep.GetType()))
            {
                cpfRep.MdiParent = this;
                cpfRep.Show();
            }
        }

        private void seedToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SeedForm seedsForm = new SeedForm();
            if (!ContainsForm(seedsForm.GetType()))
            {
                seedsForm.MdiParent = this;
                seedsForm.Show();
            }
        }

        private void usersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UsersForm usersForm = new UsersForm();
            if (!ContainsForm(usersForm.GetType()))
            {
                usersForm.MdiParent = this;
                usersForm.Show();
            }
        }

        private void jornalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            JornalForm jForm = new JornalForm();
            if (!ContainsForm(jForm.GetType()))
            {
                jForm.MdiParent = this;
                jForm.Show();
            }
        }


    }
}
